﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using System.Drawing;
using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product.Atl {
    public partial class List : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        String AltKey = null;
        String CategoryKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            AltKey = Request.QueryString["t"];
            CategoryKey = Request.QueryString["cid"];
            if (!Page.IsPostBack) {
                InitForm();
                LoadAtlList();
            }
        }

        private void LoadAtlList() {
            String hql = "from VProductAtlEO eo ";
            if(!String.IsNullOrEmpty(this.hid_CategoryId.Value)){
                hql += " where eo.CategoryKey='" + this.hid_CategoryId.Value + "'";
            }
            hql += " order by eo.CategoryKey ";
            this.gvSubList.DataSource = new ArrayList();
            this.gvSubList.DataBind();
            IList<VProductAtlEO> list = biz.Find<VProductAtlEO>(hql);
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();

            if (String.IsNullOrEmpty(AltKey)) {
                if (list != null && list.Count > 0) {
                    GvHelper.GvSelected(ref this.gvlist, list[0].AtlKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                    LoadOptList(list[0].AtlKey);
                    this.hid_SelectedKey.Value = list[0].AtlKey;
                } else {
                    this.btn_value_add.Enabled = false;
                    this.btn_value_delete.Enabled = false;
                    this.btn_value_update.Enabled = false;
                }
            } else {
                this.hid_SelectedKey.Value = AltKey;
                LoadOptList(AltKey);
                GvHelper.GvSelected(ref this.gvlist, AltKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }

        }

        private void InitForm() {
            if (!String.IsNullOrEmpty(CategoryKey)) {
                this.txt_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, CategoryKey, " / ");
                this.hid_CategoryId.Value = CategoryKey;
            } else {
                this.txt_CategoryName.Text = "所有分类";
            }
        }


        private void LoadOptList(String rTypeKey) {
            this.hid_SelectedKey.Value = rTypeKey;
            try {
                this.gvSubList.DataSource = biz.LoadAltOptions(rTypeKey);
                this.gvSubList.DataBind();
            } catch { }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvlist.PageIndex = e.NewPageIndex;
            LoadAtlList();
        }

        protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                //String key = gvlist.DataKeys[e.Row.RowIndex].Values[0].ToString();
                //string url = "Admin/Content/Code/_CodeList.aspx";
                //e.Row.Attributes.Add("url", url + "?ID=" + key);
                //e.Row.Attributes.Add("onclick", "return myUtil.onGridViewRowSelected(this,event,'cntrlist_container');");
            }
        }

        protected void btn_type_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditAtl.aspx?cid="+this.hid_CategoryId.Value);
        }

        protected void btn_type_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("EditAtl.aspx?cid=&ID=" + keys[0] + "");
            }
        }

        protected void btn_type_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteAlt(keys);
            BizFactory.Get<ILogBiz>().LogDelete("属性模板", keys.Count, this.Actor, Request);
            LoadAtlList();
        }

        protected void btn_value_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditOpt.aspx?cid="+this.hid_CategoryId.Value+"&t="+this.hid_SelectedKey.Value);
        }

        protected void btn_value_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvSubList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("EditOpt.aspx?cid=" + this.hid_CategoryId.Value + "&t="+this.hid_SelectedKey.Value+ "&ID=" + keys[0] + "");
            }
        }

        protected void btn_value_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvSubList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteAltOption(keys);
            BizFactory.Get<ILogBiz>().LogDelete("属性模板选择项", keys.Count, this.Actor, Request);
            this.LoadOptList(this.hid_SelectedKey.Value);
        }

        protected void gvlist_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckSubList") {
                String typeKey = e.CommandArgument.ToString();
                this.hid_SelectedKey.Value = typeKey;
                this.LoadOptList(typeKey);
                GvHelper.GvSelected(ref this.gvlist, typeKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            this.gvSubList.DataSource = new ArrayList();
            this.gvSubList.DataBind();
            if (!String.IsNullOrEmpty(this.hid_CategoryId.Value)) {
                this.txt_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, this.hid_CategoryId.Value, " / ");
            } else {
                this.txt_CategoryName.Text = "所有分类";
            }
            this.LoadAtlList();
        }

    }
}
